/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Rutas;

/**
 *
 * @author Usuario
 */
import java.io.IOException;
import java.io.Reader;
import java.util.*;

public class NeedlemanWunsch {

/**
 *
 * @author Usuario
 */

    /**
     * @param args the command line arguments
     */
    private String seq1;/*Secuencia optima*/
    private String seq2;/*Secuencia esperada*/
    public int[][] matrix;/*matriz de scores*/
    public int[][] rastreo;
    private int gap;
    private int error;
    public String fiSeq1;
    public String fiSeq2;
    
    public NeedlemanWunsch(int gap, int error){
        this.gap = gap;
        this.error = error;
    }
    
    public String getSeq1(){
        return this.seq1;
    }
    
    public String getSeq2(){
        return this.seq2;
    }
    
    public void setSeq1(String s){
        this.seq1 = s;
    }
    
    public void setSeq2(String s){
        this.seq2 = s;
    }
    
    
    private int obtenScore(char c){
        return 1;
    }
    
    private int maximo(int op1, int op2){
        if(op1>op2){
            return op1;
        }
        else
            return op2;
    }
    
    public void creaMatrix(){
        int i,j, match;
        int op1, op2, op3;
        int fil = seq2.length()+1;
        int col = seq1.length()+1;
        
        matrix = new int[fil][col];
        rastreo = new int[fil][col];
        
        
        for (i=0;i<fil;i++){
            for(j=0;j<col;j++){
                if((i==0)||(j==0)){
                    if((i==0)&&(j==0)){
                        matrix[0][0] = 0;
                    }
                    else
                        matrix[i][j] = this.error;
                }
                else{/*entonce si se calcula el nuevo score*/
                    if(seq1.charAt(j-1)==seq2.charAt(i-1)){
                        match = obtenScore(seq1.charAt(j-1));                        
                    }
                    else
                        match = this.error;
                    /*Consideramos las 3 opciones*/
                    op1 = matrix[i-1][j-1] + match;
                    op2 = matrix[i-1][j] + this.gap;/*secuencia 2*/
                    op3 = matrix[i][j-1] + this.gap;/*secuencia 1*/
                    
                    matrix[i][j] = maximo(maximo(op1,op2),op3);
                    
                    if(matrix[i][j]==op1)
                        rastreo[i][j]=0;
                    else if(matrix[i][j]==op2)
                            rastreo[i][j]=1;
                    else
                        rastreo[i][j]=-1;                       
                }
            }
        }
    }
    
    public void rastrea(){
        int l1, l2;
        String fSeq1, fSeq2;
        
        fSeq1 = "";
        fSeq2 = "";
        l1 = this.seq1.length();
        System.out.println(""+l1);
        l2 = this.seq2.length();
        System.out.println(""+l2);
        while((l1>0)&&(l2>0)){
            if(rastreo[l2][l1]==0){
                fSeq1=seq1.charAt(l1-1)+fSeq1;
                fSeq2=seq2.charAt(l2-1)+fSeq2;
                l1=l1-1;
                l2=l2-1;
            }
            else{
                if(rastreo[l2][l1]==1){
                    fSeq2=seq2.charAt(l2-1)+fSeq2;
                    fSeq1="-"+fSeq1;
                    l2 = l2 - 1;
                }
                else{
                    fSeq2="-"+fSeq2;
                    fSeq1=seq1.charAt(l1-1)+fSeq1;
                    l1 = l1 - 1;
                }
            }
        }
        this.fiSeq1 = fSeq1;
        this.fiSeq2 = fSeq2;
    }
    
    public int comparaPatron (int i){
        //retorna 1 si no se cumple el patron
        //retorna 0 si se cumple el patron
        return 0;
    }
    
    public void compara(){
        int cantParE = 0;//cantidad de paraderos a los que fue equivocadamente
        int cantParS = 0;//cantidad de paraderos saltados
        int cantPatE = 0;//cantidad de incumplimiento de patrones
        for(int i=0; i<this.fiSeq1.length();i++){
            if((fiSeq1.charAt(i)=='-')){
                cantParE = cantParE + 1;
            }
            else{
                if(fiSeq2.charAt(i)=='-'){
                    cantParS = cantParS + 1;
                }
                else{
                    if(fiSeq2.charAt(i)==fiSeq1.charAt(i)){
                        cantPatE = cantPatE + comparaPatron(i);                        
                    }
                    else{
                        cantParE = cantParE + 1;
                    }
                }
            }
        }
        System.out.println("Cantidad de Paraderos Equivocados: " + cantParE);
        System.out.println("Cantidad de Paraderos Omitidos: " + cantParS);
        System.out.println("Cantidad de Incumplimiento de Patrón: " + cantPatE);
        System.out.println("Cantidad de Incidencias: " + (cantParE+cantParS+cantPatE));
    }
}

